package com.laoluo.erp.struts.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {

        static Log log = LogFactory.getLog(BaseAction.class);

        public HttpServletRequest getRequest() {
                return ServletActionContext.getRequest();
        }

        public HttpServletResponse getResponse() {
                return ServletActionContext.getResponse();
        }

        protected <T> T readJson(Class<T> typeOfT) {
                try {
                        //String data = getRequest().getParameter("data");
                		String data = getPostData();
                		log.info(data);
                        if(data == null) return null;
                        return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create().fromJson(data, typeOfT);
                } catch (Exception e) {
                        log.error("readJson error", e);
                }
                return null;
        }
        
        protected String getPostData() throws IOException
        {
        	BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)getRequest().getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = br.readLine())!=null){
                sb.append(line);
            }
            return sb.toString();
        }
        
        protected void writeJson(Object obj) {
                getResponse().setCharacterEncoding("utf-8");
                try {
                        getResponse().getWriter().write(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create().toJson(obj));
                } catch (Exception e) {
                        log.error("writeJson error", e);
                }
        }
        
        protected void writeString(String str){
        	getResponse().setCharacterEncoding("utf-8");
        	try {
        		getResponse().getWriter().write(str);
			} catch (Exception e) {
				log.error("writeString error",e);
			}     	
        }
        
        protected String getJsonString(Object obj){
        	
        	return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create().toJson(obj);
        }
        
        protected void resultSuccess(Object data) {
                resultSuccess(null,data);
        }
        protected void resultSuccess(String message) {
                resultSuccess(message,null);
        }
        protected void resultSuccess(String message, Object data) {
                Result result = new Result();
                result.setSuccess(true);
                result.setMessage(message);
                result.setData(data);
                writeJson(result);
        }
        protected void resultSuccess(String message, Object data, int totalCount) {
                Result result = new Result();
                result.setSuccess(true);
                result.setMessage(message);
                result.setData(data);
                result.setTotal(totalCount);
                writeJson(result);
        }
        protected void resultFailed(String message) {
                Result result = new Result();
                result.setSuccess(false);
                result.setMessage(message);
                writeJson(result);
        }

        public static class Result {
                private boolean success;
                private String message;
                private Object data;
                private int total;

                public boolean isSuccess() {
                        return success;
                }

                public void setSuccess(boolean success) {
                        this.success = success;
                }

                public String getMessage() {
                        return message;
                }

                public void setMessage(String message) {
                        this.message = message;
                }

                public Object getData() {
                        return data;
                }

                public void setData(Object data) {
                        this.data = data;
                }

                public int getTotal() {
                        return total;
                }

                public void setTotal(int total) {
                        this.total = total;
                }
        }
}